package com.xpgk.mvc.statistic.stockHandoverStatistic;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.bstockHandover.StockTransfer;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.productSet.Product;
import com.xpgk.mvc.statistic.customerInfo.CustomerInfoStatisitSQL;
import com.xpgk.mvc.statistic.customerProductPurchaseAndSaleStatisic.CustomerProductPurchaseAndSaleStatisicSQL;
import com.xpgk.tools.ToolDateTime;

public class StockHandoverStatisticController extends BaseController{

	public void index() throws ParseException {
		String  sql=getSqlMy(Product.sqlId_nolcpro);
		List<Product>  pList=Product.dao.find(sql,Code.lc_ids);
		
		String sql2 = getSqlMy(StockTransfer.sqlId_selAll);
		List<StockTransfer> stockTransfers = StockTransfer.dao.find(sql2);
		
		String startTime = getPara("startTime", ToolDateTime.getMonthOneDay(new Date()));
		String endTime = getPara("endTime", ToolDateTime.format(new Date(), ToolDateTime.pattern_ymd));
		String organIds = getPara("organIds", getCUserOrganIds());
		
		setAttr("products", pList);		
		setAttr("stockTransfers", stockTransfers);	
		setAttr("startTime", ToolDateTime.getMonthOneDay(new Date()));
		render("stockHandoverStatistic.html");
	}
	
	/**
	 * 顾客列表
	 * @throws ParseException 
	 */
	public void view() throws ParseException{

		String startTime = getPara("startTime", ToolDateTime.getMonthOneDay(new Date()));
		String endTime = getPara("endTime", ToolDateTime.format(new Date(), ToolDateTime.pattern_ymd));
		String organIds = getPara("organIds", getCUserOrganIds());
		
		List<Record> result = new ArrayList<>();
		List<String> days = ToolDateTime.getDaysByStartAndEnd(startTime, endTime);
	
		if(days.size() > 0){
			for(String day : days){
				
				Record r = new Record();
				String  sql=getSqlMy(Product.sqlId_nolcpro);
				List<Product>  products=Product.dao.find(sql,Code.lc_ids);	
				if(products.size() > 0){
					for(Product product : products){
						//上午班
						String sql_product_in = getSqlMy(StockHandoverStatisticSQL.sqlId_product_in);
						List<Record> in_sum = Db.find(sql_product_in,product.getPKValue(),day,organIds);
						if(in_sum.size()>0){
							int i=0;
							for(Record inSum:in_sum){
								
								r.set(product.getStr("ids")+i+ "_gm", inSum.getFloat("storage"));
								r.set(product.getStr("ids")+i+ "_sy", inSum.getDouble("outgoing"));
								r.set(product.getStr("ids")+i+"_kc", inSum.getFloat("physicalInventory"));
								r.set("receiver", inSum.getStr("receiver"));
								r.set("datetime", inSum.getDate("date"));
								r.set("recipient", inSum.getStr("recipient"));
								r.set("differenceCause", inSum.getStr("differenceCause"));
								i++;
							}
							
						}
						
					}
				}
				if(r.toString().length() > 2 ){
					r.set("date", day);
					result.add(r);
				
					
				}
			}
		}
			
		renderJson(result);
		}
}
